package com.haier.cloudremote;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haier.cloudremote.common.RemoteManager;
import com.haier.cloudremote.common.SLog;
import com.haier.cloudremote.common.TroubleShooting;
import com.haier.cloudremote.db.DbAdapter;
import com.haier.cloudremote.protocal.CAEProtocal;
import com.haier.cloudremote.util.FileUtil;

public class StartStudyActivity extends Activity implements OnClickListener{

	private static final int GET_STUDY_STATE_FAIL = 10001;
	private static final int GET_STUDY_STATE_OK = 10002;
	private static final int CANNOT_CONNECT_CAE = 10003;
	
	private StringBuffer debugBuffer = null;// 记录debug按键
	
	private Button startStudyButton,continueStudyButton,studyAgainButton;
	private LinearLayout layout;
	
	private Boolean haveFindStb = true;
	private Boolean isActivityAlive;
	
	private TextView introTextView,stateTextView,intro1TextView;
	private LinearLayout continueStudyLayout;
	
	private Thread getStudyStateThread;
	private int lastStudyKey = 0;
	
	private Handler viewHandler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 // 设置全屏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.startstudy);
		SLog.d("ssc", "StartStudyActivity-----------onCreate()");
		
		ActivityStackControlUtil.add(this);
		isActivityAlive = true;
		
		viewInit();
		
		viewHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case GET_STUDY_STATE_FAIL:
					intro1TextView.setText(R.string.startstudy_clickstartstudy);
					startStudyButton.setVisibility(View.VISIBLE);
					startStudyButton.setEnabled(true);
					continueStudyLayout.setVisibility(View.INVISIBLE);
					stateTextView.setText(R.string.get_study_state_fail);
					
					SLog.d("ssc", "修改DB中该键的学习状态");
					DbAdapter dbAdapter = new DbAdapter(StartStudyActivity.this);
					dbAdapter.open();
					dbAdapter.initStudyState();// 初始化学习状态
					dbAdapter.close();
					
					break;
				case GET_STUDY_STATE_OK:
					intro1TextView.setText(R.string.startstudy_clickcontinuestudy);
					
					startStudyButton.setVisibility(View.INVISIBLE);
					continueStudyLayout.setVisibility(View.VISIBLE);
					stateTextView.setText(R.string.get_study_state_ok);
					break;
				case CANNOT_CONNECT_CAE:
					stateTextView.setText(R.string.cant_connect_cae);
					break;
				default:
					break;
				}
			}
			
		};
		
		
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SLog.d("ssc", "StartStudyActivity-----------onResume()");
		
		startStudyButton.setVisibility(View.VISIBLE);
		startStudyButton.setEnabled(false);
		continueStudyLayout.setVisibility(View.INVISIBLE);
		
		getStudyState();
	}


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		SLog.d("ssc", "StartStudyActivity-----------onStart()");
		Bitmap bm = FileUtil.readBitMap(this, R.drawable.study_bg2);
		layout.setBackgroundDrawable(new BitmapDrawable(bm));
	}


	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		layout.setBackgroundDrawable(null);
	}


	private void viewInit() {
		// TODO Auto-generated method stub
		layout = (LinearLayout) findViewById(R.id.startstudy_layout);
		
		introTextView = (TextView) findViewById(R.id.startstudy_introtext);
		intro1TextView = (TextView) findViewById(R.id.startstudy_introtext1);
		stateTextView = (TextView) findViewById(R.id.startstudy_statetext);
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			haveFindStb = bundle.getBoolean("haveFindStb");
		}
		if(haveFindStb){
			introTextView.setText(R.string.startstudy_testfinish);
		}else{
			introTextView.setText(R.string.startstudy_notfindstb);
		}
		startStudyButton = (Button) findViewById(R.id.startstudy_button);
		continueStudyLayout = (LinearLayout) findViewById(R.id.startstudy_continuebutton_layout);
		continueStudyLayout.setVisibility(View.INVISIBLE);
		startStudyButton.setVisibility(View.VISIBLE);
		startStudyButton.setEnabled(false);
		continueStudyButton = (Button) findViewById(R.id.startstudy_continuestudy_button);
		studyAgainButton = (Button) findViewById(R.id.startstudy_studyagain_button);
		studyAgainButton.setOnClickListener(this);
		continueStudyButton.setOnClickListener(this);
		startStudyButton.setOnClickListener(this);
	}

	/**
	 * 获取上次学习的进度
	 */
	private void getStudyState() {
		// TODO Auto-generated method stub
		getStudyStateThread = new Thread(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				while(isActivityAlive){
					SLog.d("ssc", "获取学习进度");
					lastStudyKey = CAEProtocal.getSSCState(1);
					SLog.d("ssc", "lastStudyKey==="+lastStudyKey);
					if(lastStudyKey==-2000){// socket no create
						viewHandler.sendEmptyMessage(CANNOT_CONNECT_CAE);
					}else if(lastStudyKey==0){
						viewHandler.sendEmptyMessage(GET_STUDY_STATE_FAIL);
						break;
					}else{
						viewHandler.sendEmptyMessage(GET_STUDY_STATE_OK);
						break;
					}
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						if(e!=null){
							SLog.e("ssc", "StartStudyActivity----------555-------e=="+e.getMessage());
						}
					}
				}
				
			}
			
		};
		
		if(RemoteManager.getStudyState(this)){// 上次学习已完车给你
			viewHandler.sendEmptyMessage(GET_STUDY_STATE_FAIL);
		}else{// 未完成，获取进度
			getStudyStateThread.start();
		}
		
	}
	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		isActivityAlive = false;
		 //移出管理栈
        ActivityStackControlUtil.remove(this); 

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (event.getKeyCode()) {
		case 12:// 5
			debugBuffer = new StringBuffer();
			debugBuffer.append("l");
			SLog.d("ssc", "11-----debugBuffer=="+debugBuffer);
			return false;

		case 13:// 6
			if(debugBuffer!=null){
				debugBuffer.append("o");
				SLog.d("ssc", "13-----debugBuffer=="+debugBuffer);
				return false;
			}
			break;

		case 11:// 4
			if(debugBuffer!=null){
				debugBuffer.append("g");
				TroubleShooting troubleShooting = new TroubleShooting(StartStudyActivity.this, debugBuffer);
				debugBuffer = new StringBuffer();

				return false;
			}
			
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(StartStudyActivity.this, StudyActivity.class);
		switch (v.getId()) {
		case R.id.startstudy_button:
			SLog.d("ssc", "StartStudyActivity-----------开始学习");
			intent.putExtra("lastStudyKey", 0);			
			startActivity(intent);
			break;
		case R.id.startstudy_continuestudy_button:	
			SLog.d("ssc", "StartStudyActivity-----------继续学习");
			intent.putExtra("lastStudyKey", lastStudyKey);			
			startActivity(intent);
			break;
		case R.id.startstudy_studyagain_button:
			
			// 修改DB中该键的学习状态
			SLog.d("ssc", "修改DB中该键的学习状态");
			DbAdapter dbAdapter = new DbAdapter(StartStudyActivity.this);
			dbAdapter.open();
			dbAdapter.initStudyState();// 初始化学习状态
			dbAdapter.close();
			
			SLog.d("ssc", "StartStudyActivity-----------重新学习");
			intent.putExtra("lastStudyKey", 0);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
	
	

}
